'use strict';

const db = uniCloud.database();

exports.main = async (event, context) => {
  console.log('【markRecommendMessageRead】云函数被调用，event：', event);
  
  const { messageId, userId } = event;
  
  if (!messageId || !userId) {
    return {
      code: 400,
      msg: '参数缺失'
    };
  }
  
  try {
    const recommendCollection = db.collection('recommendMessages');
    
    // 更新推荐消息的已读状态
    const result = await recommendCollection.doc(messageId).update({
      is_read: true
    });
    
    console.log('【markRecommendMessageRead】更新结果：', result);
    
    if (result.updated > 0) {
      return {
        code: 200,
        msg: '标记已读成功'
      };
    } else {
      return {
        code: 404,
        msg: '消息不存在或已标记为已读'
      };
    }
    
  } catch (error) {
    console.error('【markRecommendMessageRead】异常:', error);
    return {
      code: 500,
      msg: '标记已读失败：' + error.message
    };
  }
};